﻿using System;
using Raven;
using UnityEngine;
using Vector2 = Raven.Vector2;

public class BotDisplay : UnityDisplayObject
{
    private RavenBot _bot;
    private WeaponType _currentWeaponType = WeaponType.type_none;

    private static Vector3[] vbWeapon =
    {
        new Vector3(0, -1),
        new Vector3(10, -1),
        new Vector3(10, 1),
        new Vector3(0, 1),
    };

    private static Vector3[] rocketWeapon =
    {
        new Vector3(0, -3),
        new Vector3(6, -3),
        new Vector3(6, -1),
        new Vector3(15, -1),
        new Vector3(15, 1),
        new Vector3(6, 1),
        new Vector3(6, 3),
        new Vector3(0, 3)
    };

    private static Vector3[] shotGunWeapon =
    {
        new Vector3(0, 0),
        new Vector3(0, -2),
        new Vector3(10, -2),
        new Vector3(10, 0),
        new Vector3(0, 0),
        new Vector3(0, 2),
        new Vector3(10, 2),
        new Vector3(10, 0)
    };

    private GameObject _weaponObject;

    public BotDisplay(BaseGameEntity entity) : base(entity)
    {
        _bot = entity as RavenBot;
    }

    public override void Start()
    {
        base.Start();
        Vector2 pos = _bot.Pos();
        _gameObject.transform.position = new Vector3((float) pos.x, (float) pos.y);
        _gameObject.AddComponent<MeshFilter>().mesh = MeshFactory.BotShape();
        MeshRenderer render = _gameObject.AddComponent<MeshRenderer>();
        render.material = new Material(Shader.Find("Sprites/Default"));

        CreateBRadiusGizmos();
    }

    public override void Update()
    {
        Vector2 pos = _bot.Pos();
        _gameObject.transform.position = new Vector3((float) pos.x, (float) pos.y);

        Vector2 heading = _bot.Facing();
        Fix dot = Vector2.Dot(Vector2.right, heading);
        Fix rad = FixMath.Acos(dot) * Vector2.right.Sign(heading);

        _gameObject.transform.rotation = Quaternion.AngleAxis((float) FixMath.RadsToDegs(rad), Vector3.forward);

        RenderWeapon();
    }

    private void CreateBRadiusGizmos()
    {
        GameObject obj = new GameObject("BRadius");
        obj.transform.SetParent(_gameObject.transform);
        obj.AddComponent<MeshFilter>().mesh = MeshFactory.CycleMesh(Vector3.zero, (float) _bot.BRadius(), Color.red);
        MeshRenderer render = obj.AddComponent<MeshRenderer>();
        render.material = new Material(Shader.Find("Sprites/Default"));
    }

    private void RenderWeapon()
    {
        WeaponType weaponType = _bot.GetWeaponSys().GetCurrentWeapon().GetWeaponType();
        if (_currentWeaponType != weaponType)
        {
            if (_weaponObject != null)
            {
                GameObject.DestroyImmediate(_weaponObject);
            }

            _currentWeaponType = weaponType;

            _weaponObject = new GameObject(weaponType.ToString());
            _weaponObject.transform.SetParent(_gameObject.transform);
            _weaponObject.transform.localPosition = Vector3.zero;
            _weaponObject.transform.localRotation = Quaternion.identity;
            Color color = Color.white;
            Vector3[] vb = vbWeapon;

            if (_currentWeaponType == WeaponType.type_blaster)
            {
                color = Color.green;
            }
            else if (_currentWeaponType == WeaponType.type_rail_gun)
            {
                color = Color.red;
            }
            else if (_currentWeaponType == WeaponType.type_rocket_launcher)
            {
                color = Color.yellow;
                vb = rocketWeapon;
            }
            else if (_currentWeaponType == WeaponType.type_shotgun)
            {
                color = Color.cyan;
                vb = shotGunWeapon;
            }

            _weaponObject.AddComponent<MeshFilter>().mesh = MeshFactory.CloseShape(vb, color);
            MeshRenderer render = _weaponObject.AddComponent<MeshRenderer>();
            render.material = new Material(Shader.Find("Sprites/Default"));
        }
    }
}